/*
 * Copyright (C) 2009 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.demomaster.quickui_sample.ui.activity.appPicker;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.view.adapter.SimpleRecycleAdapter;
import com.demomaster.quickcompat.widget.button.QuickToggleButton;

import java.util.ArrayList;
import java.util.List;

import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import com.demomaster.quickcompat.widget.toast.QdToast;
import cn.demomaster.quickui_sample.R;
import cn.demomaster.quickui_sample.model.AppInfo;


@ActivityPager(name = "AppPicker", preViewClass = QuickToggleButton.class, resType = ResType.Resource)//, iconRes = R.drawable.ic_apps)
public final class AppPickerActivity extends QuickFragment {

    private AsyncTask<Object, Object, List<AppInfo>> backgroundTask;

    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(cn.demomaster.quickui.R.layout.list_content_simple, null);
        return view;
    }

    RecyclerView recyclerView;
    SimpleRecycleAdapter adapter;
    List<AppInfo> data = new ArrayList<>();

    @Override
    public void initView(View rootView) {
        recyclerView = findViewById(cn.demomaster.quickui.R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new SimpleRecycleAdapter(getActivity(), R.layout.app_picker_list_item, data) {
            @Override
            public void onBindView(ViewHolder holder, List data1, int position) {
                super.onBindView(holder, data, position);
                List<AppInfo> data = data1;
                TextView textView = holder.itemView.findViewById(R.id.app_picker_list_item_package);
                textView.setText(data.get(position).getPackageName() + "");
                TextView textView2 = holder.itemView.findViewById(R.id.app_picker_list_item_label);
                textView2.setText(data.get(position).getLabel() + "");

                Drawable icon = data.get(position).getIcon();
                if (icon != null) {
                    ((ImageView) holder.itemView.findViewById(R.id.app_picker_list_item_icon)).setImageDrawable(icon);
                }
            }
        };
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                QdToast.show(getContext(),position+"");
            }
        });
        recyclerView.setAdapter(adapter);
        backgroundTask = new LoadPackagesAsyncTask(getActivity()) {
            @Override
            public void onPostExecute(List<AppInfo> results) {
                super.onPostExecute(results);
                data.clear();
                data.addAll(results);
                adapter.notifyDataSetChanged();
            }
        };
        backgroundTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

//    ListAdapter listAdapter = new ArrayAdapter<AppInfo>(getActivity(),
//            R.layout.app_picker_list_item,
//            R.id.app_picker_list_item_label,
//            results) {
//      @Override
//      public View getView(int position, View convertView, ViewGroup parent) {
//        View view = super.getView(position, convertView, parent);
//        Drawable icon = results.get(position).getIcon();
//        if (icon != null) {
//          ((ImageView) view.findViewById(cn.demomaster.quickzxing.R.id.app_picker_list_item_icon)).setImageDrawable(icon);
//        }
//        return view;
//      }
//    };
    }

    void onPause1() {
        AsyncTask<?, ?, ?> task = backgroundTask;
        if (task != null) {
            task.cancel(true);
            backgroundTask = null;
        }
        super.onPause();
    }
//
//  public void onListItemClick(ListView l, View view, int position, long id) {
//    Adapter adapter = getListAdapter();
//    if (position >= 0 && position < adapter.getCount()) {
//      String packageName = ((AppInfo) adapter.getItem(position)).getPackageName();
//      Intent intent = new Intent();
//      intent.addFlags(Intents.FLAG_NEW_DOC);
//      intent.putExtra("url", "market://details?id=" + packageName); // Browser.BookmarkColumns.URL
//      setResult(RESULT_OK, intent);
//    } else {
//      setResult(RESULT_CANCELED);
//    }
//    //finish();
//  }

}
